/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fy.mybatis;

import java.io.InputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 *
 * @author Administrator
 */
public class MyDBUtil_MyBatis {

    private static MyDBUtil_MyBatis myDBUtil_MyBatis;

    private static SqlSessionFactory factory;

    private static int state = 1;

    private static Log log = LogFactory.getLog(MyDBUtil_MyBatis.class);

    private MyDBUtil_MyBatis() {
    }

    public static void setState(int state) {
        MyDBUtil_MyBatis.state = state;
    }

    public static MyDBUtil_MyBatis getInstance() {
        if (myDBUtil_MyBatis == null) {
            synchronized (MyDBUtil_MyBatis.class) {
                if (myDBUtil_MyBatis == null) {
                    myDBUtil_MyBatis = new MyDBUtil_MyBatis();
                    myDBUtil_MyBatis.init();
                }
            }
        }
        return myDBUtil_MyBatis;
    }

    private void init() {
        try {
            InputStream fi = MyDBUtil_MyBatis.class.getClassLoader().getResourceAsStream(state == 1 ? "fy/mybatis/zc_mybatis_conf.xml" : "fy/mybatis/zc_mybatis_conf_test.xml");
            factory = new SqlSessionFactoryBuilder().build(fi);
            fi.close();
            fi = null;
        } catch (Exception e) {
            log.error("MyDBUtil_MyBatis.init 3", e);
        }
    }

    public SqlSessionFactory getFactory() {
        return factory;
    }
}
